﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SqlHelp;
using CommonCs;

namespace DataServer
{
    public class BugTypeData
    {
        public DataTable GetAlltypes()
        {
            String sql = "select * from BugTypes";

            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.GetDataTableNoParm(sql);
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;

                }
                else
                {
                    return dt;
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "获取所有bugtype异常,msg:[" + Exceptions.getallbugtype + "]!");
                throw new Exception(Exceptions.getallbugtype);
            }
        }

        public bool AddType(string val)
        {
            String sql = "insert into BugTypes(TypeText) values(@val)";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@val",val)
            };
            int num = 0;
            try
            {
                num = SQLHELP.GetNonQueryWithParm(sql, parm);
                if (num == 1)
                {
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "添加bugtype[" + val + "]异常,msg:[" + Exceptions.addbugtype + "]!");
                throw new Exception(Exceptions.addbugtype);
            }
        }

        public bool UpdateType(string id, string val)
        {
            String sql = "update BugTypes set TypeText=@TypeText where TypeId=@TypeId";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@TypeText",val),
                new SqlParameter("@TypeId",id)
            };
            int num = 0;
            try
            {
                num = SQLHELP.GetNonQueryWithParm(sql, parm);
                if (num == 1)
                {
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "更新bugtype[" + val + "]异常,msg:[" + Exceptions.updatebugtype + "]!");
                throw new Exception(Exceptions.updatebugtype);
            }
        }

        public bool DeleteType(string id) 
        {
            String sql = "delete BugTypes where TypeId=@TypeId";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@TypeId",id)
            };
            int num = 0;
            try
            {
                num = SQLHELP.GetNonQueryWithParm(sql, parm);
                if (num == 1)
                {
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "删除bugtype[" + id + "]异常,msg:[" + Exceptions.deletebugtype + "]!");
                throw new Exception(Exceptions.deletebugtype);
            }
        }
    }
}
